package com.finance.system.imports;


import com.finance.base.enums.StateEnum;
import com.finance.base.exception.BaseException;
import com.finance.system.bean.pojo.User;
import com.finance.system.service.IUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import java.util.*;
import java.util.concurrent.Callable;

/**
 * Created by gyw on 2017/5/22.
 */
public class ImportCallable implements Callable<Map> {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    private static List<User> userAdd;
    private static IUserService userService;
    private static Long creatUid;

    public ImportCallable(List<User> userAdd, Long creatUid, IUserService userService){
        this.userAdd = userAdd;
        this.creatUid = creatUid;
        this.userService = userService;
    }

    @Override
    public Map call() throws BaseException{
        Map resultMap = new HashMap();
        List<String> msgList = new ArrayList<String>();
        List<User> errList = new ArrayList<User>();
        List<String> errMsg = new ArrayList<String>();
        logger.info("执行的list" + userAdd);
        for (User user : userAdd) {
            if(msgList == null || msgList.size() == 0) {
                try {
                    msgList = validate(user);
                    user.setCreateUid(creatUid);
                    user.setState(StateEnum.Valid.getState());
                    userService.add(user, null);
                } catch (BaseException e) {
                    logger.error(e.getMessage(),e);
                    errMsg.add(e.getErrorMsg());
                    errList.add(user);
                } catch (Exception e) {
                    logger.error(e.getMessage(),e);
                    errMsg.add(e.getMessage());
                    errList.add(user);
                } finally {

                }
            }else{
                StringBuilder msgStr = new StringBuilder();
                for(String msg : msgList){
                    msgStr.append(msg + "||");
                }
                msgStr.replace(msgStr.lastIndexOf("||"),msgStr.length(),"");
                errMsg.add(msgStr.toString());
                errList.add(user);
            }
        }
        resultMap.put("errMsg",errMsg);
        resultMap.put("errList",errList);
        return resultMap;
    }

    /**
     * 验证参数
     * */
    private static ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
    public static <T> List<String> validate(T t) {
        Validator validator = factory.getValidator();
        Set<ConstraintViolation<T>> constraintViolations = validator.validate(t);
        List<String> messageList = new ArrayList<>();
        for (ConstraintViolation<T> constraintViolation : constraintViolations) {
            messageList.add(constraintViolation.getMessage());
        }
        return messageList;
    }
}
